"use strict"  // Use strict JavaScript mode
    
// Pull in the modules we're going to use
var cocos  = require('cocos2d')   // Import the cocos2d module
  , nodes  = cocos.nodes          // Convenient access to 'nodes'
  , events = require('events')    // Import the events module
  , geom    = require('geometry')  // Import the geometry module
  , ccp    = geom.ccp             // Short hand to create points
  ,Texture2D = require('cocos2d/Texture2D').Texture2D
  , pause_scene = require('/pause_scene')
  
// Convenient access to some constructors
var Layer    = nodes.Layer
  , Scene    = nodes.Scene
  , Label    = nodes.Label
  , Director = cocos.Director
  , Animation        = cocos.Animation
  , Animate = cocos.actions.Animate
  , AnimationCache   = cocos.AnimationCache
  , Sequence = cocos.actions.Sequence
  , SpriteFrameCache = cocos.SpriteFrameCache
  , Sprite = cocos.nodes.Sprite

function ingame_scene(){
	        ingame_scene.superclass.constructor.call(this)
	
		this.isKeyboardEnabled = true
		this.keysDown = new Array()
		this.scheduleUpdate()
	
		// Add a background sprite.
		var sprite_bg = new cocos.nodes.Sprite({
		    file: '/resources/fps_0.png',
		    rect: new geom.Rect(0,0, 655,516)
		});
		 
		sprite_bg.anchorPoint = new geom.Point(0, 0);
		sprite_bg.position = new geom.Point(-10,-7);
		this.addChild({child: sprite_bg});
		this.contentSize = sprite_bg.contentSize;

		
		//**Health Bar*******************************************************************************
		this.healthPercent = 1.0;
		this.healthRate = 0.02;
		var scale = 0.4;
		var posX = 0;
		var posY = 380;
		
		var sprite_health_container = new cocos.nodes.Sprite({
		    file: '/resources/bar_health_2/healthbar_container.png',
		    rect: new geom.Rect(0,0, 800,300)
		});
		 
		
		sprite_health_container.anchorPoint = new geom.Point(0, 0);
		sprite_health_container.position = new geom.Point(posX,posY);
		sprite_health_container.scale =  scale;
		this.addChild({child: sprite_health_container});
		
		var sprite_health_back = new cocos.nodes.Sprite({
		    file: '/resources/bar_health_2/healthbar_back.png',
		    rect: new geom.Rect(0,0, 800,300)
		});
		 
		sprite_health_back.anchorPoint = new geom.Point(0, 0);
		sprite_health_back.position = new geom.Point(posX,posY);
		sprite_health_back.scale =  scale;
		this.addChild({child: sprite_health_back});
		
		this.sprite_health_front = new cocos.nodes.Sprite({
		    file: '/resources/bar_health_2/healthbar_front.png',
		    rect: new geom.Rect(0,0, 800,300)
		});
		 
		this.sprite_health_front.anchorPoint = new geom.Point(0, 0);
		this.sprite_health_front.position = new geom.Point(posX,posY);
		this.sprite_health_front.scale =  scale;
		this.addChild({child: this.sprite_health_front});
		
		//*******************************************************************************************
		
		
		// Twirl Cage Animation *******************************************************************************
		this.twirlRate = 0.05
		this.sprite_twirl_x = 32
		this.sprite_twirl_y = 440
		
		var texture = new Texture2D({file: module.dirname + "/resources/animations/twirlcage.png"})

		this.animFrames_twirl= []
		
		var index = 0,
			numFrames = 60,
			width = 512,
			xpos = 0,
			ypos = 0,
			frameSize = 60
		while( index < numFrames ){
			
			var frame = new cocos.SpriteFrame({texture: texture, rect: new geom.Rect(xpos, ypos, frameSize, frameSize)})
			this.animFrames_twirl.push( frame )
			console.log('pushed on frame: ' + index );
			
			xpos += frameSize;
			index += 1;
			if(xpos + frameSize > width) {
				xpos = 0;
				ypos += frameSize;
			}
		}
		
		this.sprite_twirl = new cocos.nodes.Sprite({frame:  this.animFrames_twirl[0] })
		this.sprite_twirl.position = new geom.Point(this.sprite_twirl_x,this.sprite_twirl_y)
		this.addChild(this.sprite_twirl)

		var animation = new cocos.Animation({frames: this.animFrames_twirl, delay: this.twirlRate}),
		    animate   = new cocos.actions.Animate({animation: animation, restoreOriginalFrame: false}),
		    seq       = new cocos.actions.Sequence({actions: [animate]});

		this.sprite_twirl.runAction(new cocos.actions.RepeatForever(seq))
		//*******************************************************************************************
}


ingame_scene.inherit(cocos.nodes.Layer, {
    _title: 'No title',
    subtitle: null,
    keysDown: null,
    healthPercent: null,
    healthRate: null,
    sprite_health_front: null,
    animFrames_twirl: null,
    sprite_twirl: null,
    sprite_twirl_x: null,
    sprite_twirl_y: null,
    twirlRate: null,
    
    get title () {
        return this._title
    },

    set title (newTitle) {
        this._title = newTitle
    },

    restartCallback: function () {
        var director = cocos.Director.sharedDirector

        var scene = new nodes.Scene()
        var action = restartAction()
        scene.addChild({ child: new action() })

        director.replaceScene(scene)
    },

    backCallback: function () {
        var director = cocos.Director.sharedDirector

        var scene = new nodes.Scene()
        var action = backAction()
        scene.addChild({ child: new action() })

        director.replaceScene(scene)
    },

    nextCallback: function () {
        var director = cocos.Director.sharedDirector

        var scene = new nodes.Scene()
        var action = nextAction()
        scene.addChild({ child: new action() })

        director.replaceScene(scene)
    },
    
    toTitleCallback: function (){
	console.log( 'derp' );
    },
    toPausedCallback: function (){
        var scene = new Scene()
          , layer = new pause_scene()

        // Add our layer to the scene
        scene.addChild(layer)

        // Run the scene
        cocos.Director.sharedDirector.pushScene(scene)
    },
    
    // Keyboard events
    keyDown: function (evt) {
        var kc = evt.keyCode;
	console.log('down keycode' + kc );
	    
        this.keysDown[kc] = new Boolean(true);
	    
	// Check for 'enter'
	if( this.keysDown[13] == true){
		var scene = new Scene()
		  , layer = new pause_scene()

		// Add our layer to the scene
		scene.addChild(layer)

		// Run the scene
		cocos.Director.sharedDirector.pushScene(scene)
	}
	    
	// Check for 'left'
	if( this.keysDown[37] == true){
		console.log( 'health decrease' );
		
		// Decrease our health.....................................
		this.healthPercent -= this.healthRate;
		this.healthPercent = Math.max(this.healthPercent,0.0);
		
		this.removeChild({child: this.sprite_health_front});
		
		var scale = 0.4;
		var posX = 0;
		var posY = 380;
		this.sprite_health_front = new cocos.nodes.Sprite({
		    file: '/resources/bar_health_2/healthbar_front.png',
		    rect: new geom.Rect(0,0, 800*this.healthPercent,300)
		});
		this.sprite_health_front.anchorPoint = new geom.Point(0, 0);
		this.sprite_health_front.position = new geom.Point(posX,posY);
		this.sprite_health_front.scale =  scale;
		
		this.addChild({child: this.sprite_health_front});
		//......................................................................
		
		// adjust out twirl speed........................
		
		
		this.removeChild(this.sprite_twirl)
		this.sprite_twirl = new cocos.nodes.Sprite({frame:  this.animFrames_twirl[0] })
		this.sprite_twirl.position = new geom.Point(this.sprite_twirl_x,this.sprite_twirl_y)
		this.addChild(this.sprite_twirl)

		var animation = new cocos.Animation({frames: this.animFrames_twirl, delay: this.twirlRate * this.healthPercent}),
		    animate   = new cocos.actions.Animate({animation: animation, restoreOriginalFrame: false}),
		    seq       = new cocos.actions.Sequence({actions: [animate]});

		this.sprite_twirl.runAction(new cocos.actions.RepeatForever(seq))
		
		//....................................................
	}
	// Check for 'right'
	if( this.keysDown[39] == true){
		console.log( 'health increase' );
		
		
		this.healthPercent += this.healthRate;
		this.healthPercent = Math.min(this.healthPercent,1.0);
		
		this.removeChild({child: this.sprite_health_front});
		
		var scale = 0.4;
		var posX = 0;
		var posY = 380;
		this.sprite_health_front = new cocos.nodes.Sprite({
		    file: '/resources/bar_health_2/healthbar_front.png',
		    rect: new geom.Rect(0,0, 800*this.healthPercent,300)
		});
		this.sprite_health_front.anchorPoint = new geom.Point(0, 0);
		this.sprite_health_front.position = new geom.Point(posX,posY);
		this.sprite_health_front.scale =  scale;
		
		this.addChild({child: this.sprite_health_front});
		
		
		// adjust out twirl speed........................
		
		this.removeChild(this.sprite_twirl)
		this.sprite_twirl = new cocos.nodes.Sprite({frame:  this.animFrames_twirl[0] })
		this.sprite_twirl.position = new geom.Point(this.sprite_twirl_x,this.sprite_twirl_y)
		this.addChild(this.sprite_twirl)

		var animation = new cocos.Animation({frames: this.animFrames_twirl, delay: this.twirlRate * this.healthPercent}),
		    animate   = new cocos.actions.Animate({animation: animation, restoreOriginalFrame: false}),
		    seq       = new cocos.actions.Sequence({actions: [animate]});

		this.sprite_twirl.runAction(new cocos.actions.RepeatForever(seq))
		
		//....................................................
	}
	    
    },
    keyUp: function (evt) {
        var kc = evt.keyCode;
	console.log('up keycode' + kc );
	    
        this.keysDown[kc] = new Boolean(false)
	
    },
    update: function (dt) {
	    
	
    }
})

module.exports = ingame_scene